﻿#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QJsonObject>
#include <QJsonArray>
#include "volumetool.h"
#include <QDir>
#include <QFileDialog>
#include <music.h>
#include <musiclist.h>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QPropertyAnimation>
#include <QSqlDatabase>
#include <QSystemTrayIcon>
#include <QMenu>
#include "commonpage.h"
#include "lrcpage.h"

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

    void initUi();
    void initPlayer();
    void initSqlite();

    void mouseMoveEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);

    void connectSignalAndSlot();

    QJsonArray randomPicture();//设置随机图片

    void initMusicList();

    void updateBtformAnimal();

    void quitMymusic();
public slots:
    void setCurrentUser(const QString& username);

private slots:
    void on_quit_clicked();
    void onBtFormClick(int pageId);

    void on_volume_clicked();

    void on_addlocal_clicked();

    void onUpdataLikeMusic(bool isLike,QString musicId);//对commonPage发射的updataLikeMusic信号进行响应

    void playMusic();

    void onPlayStateChanged();

    void onPlayUpClicked();//上一首

    void onPlayDownClicked();//下一首

    void onPlayAll(PageType pageType);
    void playAllMusicOfCommanPage(CommonPage* page,int index);

    void onPlaybackModeCliked();//点击切换播放模式
    void onPlaybackModeChanged(QMediaPlaylist::PlaybackMode playbackMode);

    void onPlayMusicByIndex(CommonPage* page,int index);

    void onCurrentIndexChanged(int index);

    void setMusicSilence(bool isMuted);

    void onSetMusicVolume(int volume);

    void onDurationChanged(qint64 duration);

    void onPositionChanged(qint64 duration);

    void onMetaDataAvailableChanged(bool);//获取歌曲和歌手名称

    void onMusicSliderChanged(float value); // 进度条改变

    void onLrcWordClicked();//歌词

    void on_skin_clicked();

    void on_max_clicked();

    void on_min_clicked();

private:
    Ui::Widget *ui;

    QPoint dragPosition;
    VolumeTool* volumeTool;

    MusicList musicList;

    QMediaPlayer* player;

    QSqlDatabase sqlite;
    // 要多⾸歌曲播放，以及更复杂的播放设置，需要给播放器设置媒体列表
    QMediaPlaylist* playList;

    CommonPage* currentPage;

    int currentMusicIndix;

    qint64 totalDuration;

    LrcPage* lrcPage;

    QPropertyAnimation* lrcAnimation;

    bool isDrag; // 为true时候窗⼝才能拖拽

    QString currentUser;
    QSqlDatabase userDb;

    void loadUserMusicData(); // 加载用户音乐数据
    void saveUserMusicData(); // 保存用户音乐数据
    void updateUserMusicStatus(const QString& musicId, bool isLike, bool isHistory); // 更新用户音乐状态
};

#endif // WIDGET_H
